<template>
  <div class="zp-content">
    <div class="header-zp">事件指派</div>
    <p class="line"></p>
    <div class="map-content">
      <mapCom :eventInfo="eventInfo" @changeGd="handleChangeGd"></mapCom>
      <!-- <img
        style="width: 100%; height: 100%"
        src="../../assets/images/files/map.png"
      /> -->
    </div>
    <el-form
      ref="ruleFormRef"
      :model="formValue"
      label-width="auto"
      class="ruleForm"
    >
      <el-form-item label="指派处置人员" prop="assignedMemberId">
        <el-select
          v-model="formValue.assignedMemberId"
          placeholder="请选择"
          style="width: 200px"
          clearable
          filterable
        >
          <el-option
            v-for="item in czrylist"
            :key="item.memberId"
            :label="item.memberName"
            :value="item.memberId"
          />
        </el-select>
        <div class="confirm-btn" @click="confirm">确认指派</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { dispatchEvent, getczryList } from "@/api/event";
import { onMounted, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { TabbarList } from "@/pinia/tabbar.js";
import { ElMessage } from "element-plus";
import mapCom from "./map.vue";
// Pinia 和路由
const piniaTab = TabbarList();
const route = useRoute();
const router = useRouter();

// 定义 props
const props = defineProps({
  eventInfo: Object,
});

// 定义 reactive 数据
const formValue = ref({
  assignedMemberId: "",
});
const czrylist = ref([]);
const czref = ref();

// 获取处置人员列表
const getList = async () => {
  console.log("eventInfo", props.eventInfo.busizEventId);
  try {
    const { data } = await getczryList();
    czrylist.value = data;
  } catch (e) {
    console.error("获取处置人员列表失败:", e);
    // 可以在这里提示用户错误信息
  }
};
const handleChangeGd = (data) => {
  console.log("接收到的数据:", data);

  if (data && data.userId) {
    // 检查该 userId 是否存在于处置人员列表中
    const userExists = czrylist.value.some(
      (item) => item.memberId === data.userId
    );

    if (userExists) {
      formValue.value.assignedMemberId = data.userId;
    } else {
      console.warn("接收到的用户ID不在处置人员列表中:", data.userId);
      // 可以在这里提示用户错误信息
      ElMessage.error("接收到的用户ID不在处置人员列表中");

      // 可以选择清空
      formValue.value.assignedMemberId = "";
    }
  }
};

// 确认指派方法
const confirm = async () => {
  // 检查处置人员是否已选择
  if (!formValue.value.assignedMemberId) {
    ElMessage.error("请选择处置人员");
    return;
  }

  try {
    let params = {
      ...formValue.value,
      busizEventId: props.eventInfo.busizEventId, // 事件 ID
    };
    // 调用指派事件 API
    const { data } = await dispatchEvent(params);

    // 成功后清理 tab 项
    piniaTab.deleted({
      path: route.path,
    });

    // router.push("/fplist"); // 假设跳转到指派成功页面
    piniaTab.add({
      name: "指派列表",
      path: "/fplist",
      icon: "",
      query: {},
      meta: {
        hidden: false,
        title: "",
      },
    });
    console.log("事件指派成功:", data);
  } catch (e) {
    console.error("事件指派失败:", e);
    // 可以在这里提示用户错误信息，例如弹窗
  }
};

// 页面加载时获取处置人员列表
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.zp-content {
  width: 100%;
  height: 100%;
  padding: 36px 25px 0 35px;

  .header-zp {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 16px;
    color: #333333;
    line-height: 22px;
  }
  .line {
    width: 100%;
    height: 1px;
    margin: 18px 0;
    background: rgba(34, 99, 191, 0.2);
  }
  .confirm-btn {
    margin-left: 18px;
    min-width: 143px;
    height: 36px;
    background: #2263bf;
    border-radius: 4px;
    font-size: 12px;
    color: #ffffff;
    line-height: 36px;
    margin-top: 40px;
    cursor: pointer;
    text-align: center;
  }
  .map-content {
    width: 460px;
    height: 257px;
    margin-bottom: 32px;
  }
}
</style>
